﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GrupyzServices.BdAccess;


namespace PreBuildGrupyz.Controllers
{
    public class ComentariosController : Controller
    {

        IComentarioRepository _repository;

        public ComentariosController()
            : this(new ComentarioRepository())
        { }

        public ComentariosController(ComentarioRepository repository)
        {
            _repository = repository;
        }

        //
        // GET: /Comentarios/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Deal(int id,int? pag)
        {
            ViewData["DealId"] = id;
            return View(_repository.GetComentariosDeal(id,pag));
        }

        [Authorize]
        [HttpPost]
        public ActionResult Deal(FormCollection collection)
        {
            _repository.CreateComentario(collection);

            return RedirectToAction("Deal", "Comentarios", new { id = int.Parse(collection["DealId"]) });
        }


        [ChildActionOnly]
        public ActionResult LastComment(int id)
        {
            LastCommentViewModel model;
            var comment= _repository.GetUltimoComentarioDeal(id);

            if (comment != null)
            {
                model = new LastCommentViewModel
                {
                    Imagem = comment.Cliente.UriImagem,
                    Mensagem = comment.Texto,
                    Total = _repository.GetComentarioDealCount(id),
                    DealId = id
                };
            }
            else
            {
                model = new LastCommentViewModel
                {
                    Imagem = "~/Content/images/ecyu2wwn.gif",
                    Mensagem = "Sê o primeiro a fazer um comentário!",
                    Total = 0,
                    DealId = id
                };
            }

            return PartialView("LastComment",model);
        }

    }
}
